{{/* Hugo Blox: Collection */}}
{{/* Documentation: https://hugoblox.com/blocks/ */}}
{{/* License: https://github.com/HugoBlox/hugo-blox-builder/blob/main/LICENSE.md */}}

{{/* Initialise */}}
{{ $page := .wcPage }}
{{ $block := .wcBlock }}

{{ $items_offset := $block.Params.content.offset | default 0 }}
{{ $items_count := $block.Params.content.count }}
{{ if eq $items_count 0 }}
  {{ $items_count = 65535 }}
{{ else }}
  {{ $items_count = $items_count | default 5 }}
{{ end }}

{{/* Query */}}
{{ $query := site.RegularPages }}
{{ $archive_page := "" }}

{{/* Filters */}}
{{ if $block.Params.content.page_type }}
  {{ $query = where $query "Type" $block.Params.content.page_type }}
  {{ $archive_page = site.GetPage "Section" $block.Params.content.page_type }}
{{ end }}
{{ if $block.Params.content.filters.folders }}
  {{ $folders := $block.Params.content.filters.folders }}
  {{ $query = where $query "Section" "in" $folders }}
  {{/* Init archive page to main folder */}}
  {{ $main_folder := index $folders 0 }}
  {{ $archive_page = site.GetPage "Section" $main_folder }}
{{ end }}
{{ if $block.Params.content.filters.tag }}
  {{ $archive_page = site.GetPage (printf "tags/%s" (urlize $block.Params.content.filters.tag)) }}
  {{ $query = $query | intersect $archive_page.Pages }}
{{ end }}
{{ if $block.Params.content.filters.category }}
  {{ $archive_page = site.GetPage (printf "categories/%s" (urlize $block.Params.content.filters.category)) }}
  {{ $query = $query | intersect $archive_page.Pages }}
{{ end }}
{{ if $block.Params.content.filters.publication_type }}
  {{ $archive_page = site.GetPage (printf "publication_types/%s" $block.Params.content.filters.publication_type) }}
  {{ $query = $query | intersect $archive_page.Pages }}
{{ end }}
{{ if $block.Params.content.filters.author }}
  {{ $archive_page = site.GetPage (printf "authors/%s" (urlize $block.Params.content.filters.author)) }}
  {{ $query = $query | intersect $archive_page.Pages }}
{{ end }}
{{ if $block.Params.content.filters.featured_only }}
  {{ $query = where $query "Params.featured" "==" true }}
{{ end }}
{{ if $block.Params.content.filters.exclude_featured }}
  {{ $query = where $query "Params.featured" "!=" true }}
{{ end }}
{{ if $block.Params.content.filters.exclude_past }}
  {{ $query = where $query "Date" ">=" now }}
{{ end }}
{{ if $block.Params.content.filters.exclude_future }}
  {{ $query = where $query "Date" "<" now }}
{{ end }}

{{ $count := len $query }}

{{/* Sort */}}
{{ $sort_by := $block.Params.content.sort_by | default "Date" }}
{{ $sort_ascending := $block.Params.content.sort_ascending | default (eq $block.Params.content.order "asc") | default false }}
{{ $sort_order := cond $sort_ascending "asc" "desc" }}
{{ $query = sort $query $sort_by $sort_order }}

{{/* Offset and Limit */}}
{{ if gt $items_offset 0 }}
  {{ $query = first $items_count (after $items_offset $query) }}
{{ else }}
  {{ $query = first $items_count $query }}
{{ end }}

{{ $columns := $block.Params.design.columns | default "2" }}

<div class="col-12 {{if eq $columns "2"}}col-lg-8{{end}}">

  {{ with $block.Content }}{{ . }}{{ end }}

  {{ range $index, $item := $query }}
    {{ partial "functions/render_view" (dict "page" $block "item" . "view" ($block.Params.design.view | default "compact") "index" $index) }}
  {{end}}

  {{/* Archive link */}}
  {{ $show_archive_link := $block.Params.content.archive.enable | default (gt $count $items_count) }}
  {{ if $show_archive_link | and $archive_page }}

    {{ $archive_link := "" }}
    {{ if $block.Params.content.archive.link }}
      {{ $archive_link = $block.Params.content.archive.link | relLangURL }}
    {{ else }}
      {{ $archive_link = $archive_page.RelPermalink }}
    {{ end }}

    {{/* Localisation */}}
    {{ $items_type := $archive_page.Type }}
    {{ $i18n := "" }}
    {{ if eq $items_type "post" }}
      {{ $i18n = "more_posts" }}
    {{ else if eq $items_type "event" }}
      {{ $i18n = "more_talks" }}
    {{ else if eq $items_type "publication" }}
      {{ $i18n = "more_publications" }}
    {{ else }}
      {{ $i18n = "more_pages" }}
    {{ end }}

    {{ $archive_text := $block.Params.content.archive.text | default (i18n $i18n) | default "See all" }}

    <div class="see-all">
      <a href="{{ $archive_link }}">
        {{ $archive_text | emojify }}
        <i class="fas fa-angle-right"></i>
      </a>
    </div>
  {{ end }}

</div>
